package com.example.studentapp.course;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.MainActivity;
import com.example.studentapp.R;
import com.example.studentapp.beans.Course;
import com.example.studentapp.db.SQLiteDB;

import java.util.List;

public class CourseEditActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView course_edit_returnIV;
    TextView course_edit11TV, course_edit12TV, course_edit13TV, course_edit14TV, course_termTV;
    TextView course_edit21TV, course_edit22TV, course_edit23TV, course_edit24TV;
    TextView course_edit31TV, course_edit32TV, course_edit33TV, course_edit34TV;
    TextView course_edit41TV, course_edit42TV, course_edit43TV, course_edit44TV;
    TextView course_edit51TV, course_edit52TV, course_edit53TV, course_edit54TV;
    String course_name;
    int course_id;
    Intent intent;
    // 课表课程信息
    List<Course> list;
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;
    // 添加操作
    int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_edit);
        init();
        getData();
    }

    @SuppressLint("Range")
    private void getData() {
        // 读取数据库中的课程信息
        try{
            sqLiteDB = new SQLiteDB(getApplicationContext());
            sqLiteDatabase = sqLiteDB.getWritableDatabase();
            String sql = "select * from course_detail where course_list_id=" + course_id;
            Cursor cursor = sqLiteDatabase.rawQuery(sql,null);
            if(cursor.getCount() >0){
                cursor.moveToFirst();
                for(int i = 0; i < cursor.getCount();i++){
                     String name = cursor.getString(cursor.getColumnIndex("name"));
                    int id = cursor.getInt(cursor.getColumnIndex("id"));
                    int section = cursor.getInt(cursor.getColumnIndex("section"));
                    String place = cursor.getString(cursor.getColumnIndex("place"));
                    String teacher = cursor.getString(cursor.getColumnIndex("teacher"));
                    list.add(new Course(name,place,teacher,section,id,course_id));
                    cursor.moveToNext();
                }
                
                setCourseInfor(list);
            }else{
                // 查不到数据
                Toast.makeText(getApplicationContext(), getString(R.string.course_del_failed), Toast.LENGTH_SHORT).show();
            }


        }catch (Exception e){
            Toast.makeText(getApplicationContext(), getString(R.string.course_get_null), Toast.LENGTH_SHORT).show();
        }
    }

    // 所有课程信息回显到课表上
    private void setCourseInfor(List<Course> list) {
        for(int i =0;i<list.size();i++){
            switch (list.get(i).getSection()){
                case 11:
                    course_edit11TV.setText(list.get(i).getName());
                    break;
                case 12:
                    course_edit12TV.setText(list.get(i).getName());
                    break;
                case 13:
                    course_edit13TV.setText(list.get(i).getName());
                    break;
                case 14:
                    course_edit14TV.setText(list.get(i).getName());
                    break;


                case 21:
                    course_edit21TV.setText(list.get(i).getName());
                    break;
                case 22:
                    course_edit22TV.setText(list.get(i).getName());
                    break;
                case 23:
                    course_edit23TV.setText(list.get(i).getName());
                    break;
                case 24:
                    course_edit24TV.setText(list.get(i).getName());
                    break;


                case 31:
                    course_edit31TV.setText(list.get(i).getName());
                    break;
                case 32:
                    course_edit32TV.setText(list.get(i).getName());
                    break;
                case 33:
                    course_edit33TV.setText(list.get(i).getName());
                    break;
                case 34:
                    course_edit34TV.setText(list.get(i).getName());
                    break;


                case 41:
                    course_edit41TV.setText(list.get(i).getName());
                    break;
                case 42:
                    course_edit42TV.setText(list.get(i).getName());
                    break;
                case 43:
                    course_edit43TV.setText(list.get(i).getName());
                    break;
                case 44:
                    course_edit44TV.setText(list.get(i).getName());
                    break;


                case 51:
                    course_edit51TV.setText(list.get(i).getName());
                    break;
                case 52:
                    course_edit52TV.setText(list.get(i).getName());
                    break;
                case 53:
                    course_edit53TV.setText(list.get(i).getName());
                    break;
                case 54:
                    course_edit54TV.setText(list.get(i).getName());
                    break;
            }
        }
    }

    private void init() {
        course_edit_returnIV = findViewById(R.id.course_edit_returnIV);
        course_termTV = findViewById(R.id.course_termTV);
      //  intent = getIntent();
       // course_name = intent.getStringExtra("course_name");
       // course_termTV.setText(course_name);
       // course_id = intent.getIntExtra("course_id", 0);

       // list = new ArrayList<>();
        course_edit11TV = findViewById(R.id.course_edit11TV);
        course_edit12TV = findViewById(R.id.course_edit12TV);
        course_edit13TV = findViewById(R.id.course_edit13TV);
        course_edit14TV = findViewById(R.id.course_edit14TV);

        course_edit21TV = findViewById(R.id.course_edit21TV);
        course_edit22TV = findViewById(R.id.course_edit22TV);
        course_edit23TV = findViewById(R.id.course_edit23TV);
        course_edit24TV = findViewById(R.id.course_edit24TV);

        course_edit31TV = findViewById(R.id.course_edit31TV);
        course_edit32TV = findViewById(R.id.course_edit32TV);
        course_edit33TV = findViewById(R.id.course_edit33TV);
        course_edit34TV = findViewById(R.id.course_edit34TV);

        course_edit41TV = findViewById(R.id.course_edit41TV);
        course_edit42TV = findViewById(R.id.course_edit42TV);
        course_edit43TV = findViewById(R.id.course_edit43TV);
        course_edit44TV = findViewById(R.id.course_edit44TV);

        course_edit51TV = findViewById(R.id.course_edit51TV);
        course_edit52TV = findViewById(R.id.course_edit52TV);
        course_edit53TV = findViewById(R.id.course_edit53TV);
        course_edit54TV = findViewById(R.id.course_edit54TV);
        course_edit_returnIV.setOnClickListener(this);

        course_edit11TV.setOnClickListener(this);
        course_edit12TV.setOnClickListener(this);
        course_edit13TV.setOnClickListener(this);
        course_edit14TV.setOnClickListener(this);

        course_edit21TV.setOnClickListener(this);
        course_edit22TV.setOnClickListener(this);
        course_edit23TV.setOnClickListener(this);
        course_edit24TV.setOnClickListener(this);

        course_edit31TV.setOnClickListener(this);
        course_edit32TV.setOnClickListener(this);
        course_edit33TV.setOnClickListener(this);
        course_edit34TV.setOnClickListener(this);

        course_edit41TV.setOnClickListener(this);
        course_edit42TV.setOnClickListener(this);
        course_edit43TV.setOnClickListener(this);
        course_edit44TV.setOnClickListener(this);

        course_edit51TV.setOnClickListener(this);
        course_edit52TV.setOnClickListener(this);
        course_edit53TV.setOnClickListener(this);
        course_edit54TV.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.course_edit_returnIV:
                Intent intent = new Intent(CourseEditActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.course_edit11TV:
                callDialog(course_edit11TV, 11);
                break;
            case R.id.course_edit12TV:
                callDialog(course_edit12TV, 12);
                break;
            case R.id.course_edit13TV:
                callDialog(course_edit13TV, 13);
                break;
            case R.id.course_edit14TV:
                callDialog(course_edit14TV, 14);
                break;
            case R.id.course_edit21TV:
                callDialog(course_edit21TV, 21);
                break;
            case R.id.course_edit22TV:
                callDialog(course_edit22TV, 22);
                break;
            case R.id.course_edit23TV:
                callDialog(course_edit23TV, 23);
                break;
            case R.id.course_edit24TV:
                callDialog(course_edit24TV, 24);
                break;

            case R.id.course_edit31TV:
                callDialog(course_edit31TV, 31);
                break;
            case R.id.course_edit32TV:
                callDialog(course_edit32TV, 32);
                break;
            case R.id.course_edit33TV:
                callDialog(course_edit33TV, 33);
                break;
            case R.id.course_edit34TV:
                callDialog(course_edit34TV, 34);
                break;
            case R.id.course_edit41TV:
                callDialog(course_edit41TV, 41);
                break;
            case R.id.course_edit42TV:
                callDialog(course_edit42TV, 42);
                break;
            case R.id.course_edit43TV:
                callDialog(course_edit43TV, 43);
                break;
            case R.id.course_edit44TV:
                callDialog(course_edit44TV, 44);
                break;

            case R.id.course_edit51TV:
                callDialog(course_edit51TV, 51);
                break;
            case R.id.course_edit52TV:
                callDialog(course_edit52TV, 52);
                break;
            case R.id.course_edit53TV:
                callDialog(course_edit53TV, 53);
                break;
            case R.id.course_edit54TV:
                callDialog(course_edit54TV, 54);
                break;

        }
    }
    // 点击单个课程 弹出对话框
    private void callDialog(TextView courseTV, int section) {
        Dialog myDialog = new Dialog(CourseEditActivity.this);
        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = myDialog.getWindow();
        window.setContentView(R.layout.add_course_dialog);
        myDialog.show();
        EditText course_nameET = window.findViewById(R.id.course_nameET);
        EditText course_placeET = window.findViewById(R.id.course_placeET);
        EditText course_teaET = window.findViewById(R.id.course_teaET);
        Button course_okBtn = window.findViewById(R.id.course_okBtn);
        Button course_cancelBtn = window.findViewById(R.id.course_cancelBtn);
        String ci = courseTV.getText().toString();
        // 获取到再数据集list中的位置i
        int i = 0;
        if(ci.length() > 0){
            // 更新操作1
            // 回显数据到弹出框
            flag =1;
            i = getPositionList(section);
            course_nameET.setText(ci);
            course_placeET.setText(list.get(i).getPlace());
            course_teaET.setText(list.get(i).getTeacher());
        }
        course_cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myDialog.dismiss();
            }
        });
        int finalI = i;
        course_okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = course_nameET.getText().toString();
                String place = course_placeET.getText().toString();
                String tea = course_teaET.getText().toString();
                if(name.length() > 0 && place.length()>0 && tea.length() > 0){
                    if(flag == 1){
                        // 更新课程信息
                        try {
                            String updateSql="update course_detail  set name='"+name+"',place='"+place
                                    +"',teacher='"+tea+"' where id="+list.get(finalI).getId();
                            sqLiteDatabase.execSQL(updateSql);
                            courseTV.setText(name);
                            myDialog.dismiss();
                        }catch (Exception e){
                            Toast.makeText(getApplicationContext(), getString(R.string.course_update_failed), Toast.LENGTH_SHORT).show();
                        }
                    }else if(flag == 0){
                        // 添加 课程信息
                        try{
                            String insertSql="insert into course_detail(course_list_id,name,place,teacher,section) values("+course_id+",'"+name+"','"+
                                    place+"','"+tea+"',"+section+")";
                            sqLiteDatabase.execSQL(insertSql);
                            courseTV.setText(name);
                            myDialog.dismiss();
                        }catch (Exception e){
                            Toast.makeText(getApplicationContext(),
                                    getString(R.string.course_add_detail_error),Toast.LENGTH_SHORT).show();
                        }

                    }
                    myDialog.dismiss();

                }else {
                    Toast.makeText(getApplicationContext(), getString(R.string.course_toast_no), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    // 获取对应 课程list 的索引
    private int getPositionList(int section) {
        int postition =0;
        for(int i = 0; i < list.size();i++){
            if(list.get(i).getSection() == section){
                postition = i;
                break;
            }
        }
        return postition;
    }
}